<template>
  <div class="goods-page">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            v-model="query"
            @clear="getGoodList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoodPage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodTable" border style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="800">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="90">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180">
          <template #default="{ row }">
            <span>{{ (row.add_time * 1000) | relativeTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <div class="actions">
              <el-button type="primary" icon="el-icon-edit" size="mini"
                >编辑</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="delGoodsInfo(row.goods_id)"
                >删除</el-button
              >
              <el-tooltip
                effect="dark"
                content="分配角色"
                placement="top"
                :enterable="false"
              >
              </el-tooltip>
            </div> </template
        ></el-table-column>
      </el-table>
      <el-pagination
        :current-page="pagenum"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="changePage"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { delGoodsApi, getGoodsList } from "@/api/goods";

export default {
  name: "shopList",
  data() {
    return {
      pagenum: 1,
      pagesize: 10,
      total: 1,
      query: "",
      goodTable: [],
    };
  },
  created() {
    this.getGoodList();
  },
  methods: {
    async getGoodList() {
      const res = await getGoodsList({
        pagenum: this.pagenum,
        pagesize: this.pagesize,
        query: this.query,
      });
      // console.log(res);
      this.total = res.data.total;
      // console.log(this.total);
      this.goodTable = res.data.goods;
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.getGoodList();
    },
    changePage(page) {
      this.pagenum = page;
      this.getGoodList();
    },
    async delGoodsInfo(id) {
      // console.log(row);
      const resConfrm = await this.$confirm(
        "此操作将永久删除商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (resConfrm !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const res = await delGoodsApi(id);
      if (res.meta.status === 200) {
        this.$message.success("删除成功");
        this.getGoodList();
      } else {
        this.$message.error(res.meta.msg);
      }
      this.currentPage = 1;
      this.getGoodList();
    },
    addGoodPage() {
      this.$router.push("/goods/add");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-table {
  margin-bottom: 15px;
}
.el-button {
}
.el-row {
  margin-bottom: 15px;
}
</style>
